package at.ac.tuwien.swa.sparrow.peer.component

import ac.at.tuwien.infosys.swa.audio.Fingerprint
import at.ac.tuwien.swa.sparrow.common.TrackInfo
import com.typesafe.scalalogging.log4j.Logging
import org.springframework.stereotype.Component
import scala.collection.generic.Growable
import scala.collection.mutable

@Component
class MusicLibrary extends Growable[(Fingerprint, TrackInfo)] with Logging {

	private[component] val database = mutable.Map[Fingerprint, TrackInfo]()

	def +=(trackInfo: TrackInfo): MusicLibrary.this.type = +=(trackInfo.fingerPrint -> trackInfo)

	def +=(entry: (Fingerprint, TrackInfo)): MusicLibrary.this.type = {
		logger.debug(s"Adding audio file to library: ${entry._2}")
		database += entry
		this
	}

	def apply(f: Fingerprint) = database find (p => f != null && (p._1 `match` f) >= 0)

	def clear() {
		database.clear()
		logger.info("Library cleared")
	}
}
